package top.lhj.security.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import top.lhj.security.example.entity.UserEntity;
import top.lhj.security.example.service.UserService;

import java.util.List;

/**
 * @Author LiHaoJie
 * @Create 2024/2/3 10:48
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    
    @Autowired
    UserService userService;
    
    
    @PreAuthorize(value = "hasRole('ADMIN') and authentication.name == 'admin'")
    @GetMapping("/list")
    public ResponseEntity list() {
        List<UserEntity> list = userService.list();
        return ResponseEntity.ok(list);
    }
    
    @PreAuthorize(value = "hasAnyAuthority('USER_ADD')")
    @PostMapping("/add")
    public ResponseEntity add(@RequestBody UserEntity userEntity) {
        boolean b = userService.addUser(userEntity);
        return ResponseEntity.ok(b);
    }
    
}
